package com.sasis.dao.ibatis;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

public class BooleanTypeHandler implements TypeHandlerCallback {

	public Object getResult(ResultGetter getter) throws SQLException {
		int i = getter.getInt();
		if (i == 0) {
			return new Boolean(false);
		} else if (i == 1) {
			return new Boolean(true);
		}
		throw new SQLException ("Unexpected value " + i + " found where 0 or 1 was expected.");
	}

	public void setParameter(ParameterSetter setter, Object parameter)
			throws SQLException {
		boolean b = ((Boolean) parameter).booleanValue();
		if (b) {
			setter.setInt(1);
		} else {
			setter.setInt(0);
		}
	}

	public Object valueOf(String s) {
		if ("0".equalsIgnoreCase(s)) {
			return new Boolean(false);
		} else if ("1".equalsIgnoreCase(s)) {
			return new Boolean(true);
		}
		throw new RuntimeException ("Unexpected value " + s + " found where 0 or 1 was expected.");
	}

}
